package  
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author UWGameDevClub
	 */
	public class Room extends Sprite
	{
		private const SCROLLSPEED:Number = 5;
		private const SCROLLMARGIN:Number = 100;
		
		//the currently selected item, if there is one
		private var _selected:Item;
		//items in hte room
		private var _items:Array = new Array();
		
		//background of the room
		private var _bitmap:Bitmap;
		
		private var _overlay:Bitmap;
		
		public function Room( image:Class ) 
		{
			_bitmap = new image();
			addChild(_bitmap);
			
			_overlay = new Art.Overlay();
			_overlay.alpha = 0.25;
			_overlay.width = width;
			_overlay.height = height;
			addChild(_overlay);
		}
		
		public function startup():void
		{
			addEventListener(MouseEvent.CLICK, click);
			_selected = null;
			Main.main.paused = false;
			
			addEventListener(Event.ENTER_FRAME, update);
		}
		public function shutdown():void
		{
			removeEventListener(MouseEvent.CLICK, click);
			removeEventListener(Event.ENTER_FRAME, update);
			_selected = null;
			Main.main.paused = false;
		}
		
		public function add( item:Item , x:Number = 0, y:Number = 0, script:Array = null ):void
		{
			_items.push( item );
			addChild( item );
			item.x = x;
			item.y = y;
			if ( script != null ) {
				item.interact = function():void { Main.main.playScript( script ); };
			}
			
			setChildIndex(_overlay, numChildren - 1);
		}
		public function remove( item:Item ):void
		{
			removeChild( item );
			_items.splice( _items.indexOf(item), 1 );
		}
		
		public function click(event:MouseEvent):void
		{
			if ( Main.main.paused ) return;
			//check if the player clicked on an item
			var clicked:Item;
			if (event.target is Item) clicked = event.target as Item;
			else clicked = null;
			
			//make sure it was actually the image of the item that was clicked.
			if ( clicked != null && !clicked.pixelTest( clicked.mouseX, clicked.mouseY ) ) clicked = null;
			
			if (_selected != null && clicked != null) {
				//clicked on an item with another one selected; combine them
				if( clicked.combine != null ) {
					clicked.combine(_selected);
				}
				_selected.toggleSelect();
				_selected = null;
			} else {
				if (event.shiftKey) {
					//clicked on an item while holding shift; select it.
					if (clicked != null) clicked.toggleSelect();
					if (_selected != null) _selected.toggleSelect();
					_selected = clicked;
				} else {
					//interact with the item that was clicked on
					if (clicked != null && clicked.interact != null) clicked.interact();
					if (_selected != null) _selected.toggleSelect();
					_selected = null;
				}
			}
			//stop the mouse click from registering with hte main class, so that dialog doesn't immediately advance
			event.stopPropagation();
		}
		
		public function update(event:Event):void {
			if (Main.main.paused) return;
			
			if (stage.mouseX > stage.stageWidth - SCROLLMARGIN) {
				x -= SCROLLSPEED;
				if (x + width < stage.stageWidth) x = stage.stageWidth - width;
			}
			if (stage.mouseX < SCROLLMARGIN) {
				x += SCROLLSPEED;
				if (x > 0) x = 0;
			}
			if (stage.mouseY < SCROLLMARGIN) {
				y += SCROLLSPEED;
				if (y > 0) y = 0;
			}
			if (stage.mouseY > stage.stageHeight - SCROLLMARGIN) {
				y -= SCROLLSPEED;
				if (y + height < stage.stageHeight) y = stage.stageHeight - height;
			}
		}
	}

}